package view 
{
	import flash.display.Sprite;
	import model.Board;
	import model.Constants;
	import model.observer.IView;
	import model.Shapes.TetrisShape;
	import model.TetrisGame;
	/**
	 * ...
	 * @author Yang Wang
	 */
	public class GameView extends Sprite implements IView
	{
		private var m_game:TetrisGame;
		
		private var m_boardView:BoardView;
		private var m_hold:ShapeView;
		private var m_queue:Sprite;
		
		public function GameView(game:TetrisGame) 
		{
			m_game = game;
			m_game.addView(this);
			m_boardView = new BoardView(m_game.board);
			this.init();
		}
		
		public function init() : void {
			this.addChild(m_boardView);
			m_boardView.x = 100;
			m_boardView.y = 20;
			updateHold();
			updateQueue();
		}
		
		public function updateHold() : void {
			if (m_game.hold != null) {
				if (this.m_hold != null && this.contains(m_hold)) {
					this.removeChild(m_hold);
				}
				m_hold = new ShapeView(m_game.hold);

				this.addChild(m_hold);
			}
		}
		
		public function updateQueue() : void {
			if (m_queue != null && this.contains(m_queue)) {
				this.removeChild(m_queue);
			}
			
			var queue:Sprite = new Sprite();
			var y:int = 0;
			for each (var shape:TetrisShape in m_game.previewQueue) 
			{
				var shapeView:ShapeView = new ShapeView(shape);
				queue.addChild(shapeView);
				shapeView.y = y * 4 * Constants.SQUARE_DIMENSION;
				y++;
			}
			this.m_queue = queue;
			this.addChild(m_queue);
			
			this.m_queue.y = 100;
			this.m_queue.x = 400;
		}
		
		/* INTERFACE model.observer.IView */
		
		public function updateView(obj:Object):void 
		{
			updateHold();
			updateQueue();
		}
		
	}

}